package com.ben.chatroom.handler;

import com.ben.chatroom.common.entity.ProtoMsg;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;

/**
 * @author
 */
@Slf4j
@ChannelHandler.Sharable
public class ImNodeHeartBeatClientHandler extends ChannelInboundHandlerAdapter {

  private static final int HEARTEAT_INTERVAL = 50;

  public void heartBeart(ChannelHandlerContext ctx) {

  }

  /**
   * 接受到服务器的心跳回写
   */
  @Override
  public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

    if(!(msg instanceof ProtoMsg.Message)) {
      super.channelRead(ctx, msg);
      return;
    }

    ProtoMsg.Message pkg = (ProtoMsg.Message)msg;
    ProtoMsg.HeadType type = pkg.getType();

    if(ProtoMsg.HeadType.HEART_BEAT.equals(type)) {
      ProtoMsg.MessageHeartBeat messageHeartBeat = pkg.getHeartBeat();
      log.info("  收到 imNode HEART_BEAT  消息 from: " + messageHeartBeat.getJson());
      log.info("  收到 imNode HEART_BEAT seq: " + messageHeartBeat.getSeq());

    }
    else {
      super.channelRead(ctx, msg);
    }


  }
}
